package com.fresh.controller;

import com.fresh.common.ResponseResult;
import com.github.pagehelper.PageInfo;
import com.fresh.entity.Pay;
import com.fresh.entity.RawIn;
import com.fresh.entity.RawInQO;
import com.fresh.entity.RawOut;
import com.fresh.feignclient.LingLiaoFeignClient;
import com.fresh.feignclient.YingFuKuanFeignClient;
import com.fresh.service.YuanLiaoService;
import com.fresh.util.SnowflakeIdWorker;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/yuanliao")
public class YuanLiaoController {
    @Autowired
    SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    YuanLiaoService yuanLiaoService;
    @Autowired
    YingFuKuanFeignClient yingFuKuanFeignClient;
    @Autowired
    LingLiaoFeignClient lingLiaoFeignClient;

    @GetMapping({"/list/{pageNum}"})
    public Object list(RawInQO rawInQO, @PathVariable(required = false) Integer pageNum) {
        if (pageNum == null) {
            pageNum = 1;
        }
        PageInfo<RawIn> pageInfo = yuanLiaoService.findPageInfoByCondition(rawInQO, pageNum, 3);
        return new ResponseResult(200, "success", pageInfo);

    }

    @PostMapping("/add")
    public Object add(@RequestBody RawIn rawIn) {
        Date dt = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String data = sdf.format(dt);

        rawIn.setRiId(snowflakeIdWorker.nextId());
        rawIn.setRiIn(data);
        yuanLiaoService.add(rawIn);
        return new ResponseResult(200, "success", null);
    }

    @PostMapping("/update")
    public Object update(@RequestBody RawIn rawIn) {
        Date dt = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String data = sdf.format(dt);

        rawIn.setRiEnd(data);
        yuanLiaoService.update(rawIn);
        return new ResponseResult(200, "success", null);
    }

    @PostMapping("/pay")
    public Object putIn(@RequestBody Pay pay) {
        return yingFuKuanFeignClient.add(pay);
    }

    //@Transactional(rollbackFor = Exception.class)
    @GlobalTransactional(name = "fsp-create-getOut", rollbackFor = Exception.class)
    @PostMapping("/getOut")
    public Object putIn(@RequestBody RawOut rawOut) {
        Date dt = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String data = sdf.format(dt);

        Integer riNum = yuanLiaoService.getRiNum(rawOut.getCgNo());
        Integer roNum = rawOut.getRoNum();

        if (roNum > riNum) {
            return new ResponseResult(111, "库存不足", null);
        } else if (roNum / riNum == 1) {
            yuanLiaoService.changeStatus4ByCgNo(rawOut.getCgNo(), data);
            yuanLiaoService.updateRiNum(rawOut.getCgNo(), rawOut.getRoNum());
            return lingLiaoFeignClient.add(rawOut);
        } else {
            yuanLiaoService.changeStatus3ByCgNo(rawOut.getCgNo(), data);
            yuanLiaoService.updateRiNum(rawOut.getCgNo(), rawOut.getRoNum());
            return lingLiaoFeignClient.add(rawOut);
        }
    }

}
